﻿Public Class Servicios

    Private _OpcionSeleccionada As Char
    Private _p1 As String
    Private miDB As BBDD

    Sub New()
        InitializeComponent()
        _OpcionSeleccionada = "0"
    End Sub

    Sub New(ByVal OPCION As Char)
        InitializeComponent()
        _OpcionSeleccionada = OPCION
    End Sub

    Private Sub limpiar()
        tbBuscar.Text = ""
        tbDescripcion.Text = ""
        tbNombre.Text = ""
        tbPrecio.Text = ""
        tbTiempoEstimado.Text = ""
    End Sub

    Private Sub Servicios_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        miDB = New BBDD()
        dgwLista.Columns.Add("idTratamiento", "ID")
        dgwLista.Columns.Add("nombre", "Nombre")
        dgwLista.Columns.Add("descripcion", "Descrición")
        dgwLista.Columns.Add("tiempoEstimado", "Tiempo")
        dgwLista.Columns.Add("precio", "precio")
        Dim ancho As Integer = 853

        Select Case (_OpcionSeleccionada)
            Case ("0")
                PictureBoxGuardar.Visible = True

            Case ("1")
                Me.Width = ancho
                dgwLista.Visible = True
                PictureBoxBuscar.Visible = True
                tbBuscar.Visible = True
                PictureBoxModificar.Visible = True

                miDB.EjecutarComandoSelect("select * from tratamientos")
                Dim linea(5) As String
                While miDB.lector.Read()
                    linea(0) = miDB.lector("idTratamiento").ToString
                    linea(1) = miDB.lector("nombre").ToString
                    linea(2) = miDB.lector("descripcion").ToString
                    linea(3) = miDB.lector("tiempoEstimado").ToString
                    linea(4) = miDB.lector("precio").ToString
                    dgwLista.Rows.Add(linea)
                End While
                miDB.Desconectar()

            Case ("2")
                Me.Width = ancho
                dgwLista.Visible = True
                PictureBoxBuscar.Visible = True
                tbBuscar.Visible = True

                miDB.EjecutarComandoSelect("select * from tratamientos")
                Dim linea(5) As String
                While miDB.lector.Read()
                    linea(0) = miDB.lector("idTratamiento").ToString
                    linea(1) = miDB.lector("nombre").ToString
                    linea(2) = miDB.lector("descripcion").ToString
                    linea(3) = miDB.lector("tiempoEstimado").ToString
                    linea(4) = miDB.lector("precio").ToString
                    dgwLista.Rows.Add(linea)
                End While
                miDB.Desconectar()

                tbNombre.Enabled = False
                tbDescripcion.Enabled = False
                tbTiempoEstimado.Enabled = False
                tbPrecio.Enabled = False

            Case ("3")
                Me.Width = ancho
                dgwLista.Visible = True
                PictureBoxBuscar.Visible = True
                tbBuscar.Visible = True
                PictureBoxBorrar.Visible = True

                miDB.EjecutarComandoSelect("select * from tratamientos")
                Dim linea(5) As String
                While miDB.lector.Read()
                    linea(0) = miDB.lector("idTratamiento").ToString
                    linea(1) = miDB.lector("nombre").ToString
                    linea(2) = miDB.lector("descripcion").ToString
                    linea(3) = miDB.lector("tiempoEstimado").ToString
                    linea(4) = miDB.lector("precio").ToString
                    dgwLista.Rows.Add(linea)
                End While
                miDB.Desconectar()

                tbNombre.Enabled = False
                tbDescripcion.Enabled = False
                tbTiempoEstimado.Enabled = False
                tbPrecio.Enabled = False

        End Select
    End Sub

    Private Sub Insertar()
        If miDB.EjecutarComandoInsert("insert into tratamientos(nombre, descripcion,tiempoEstimado,precio) values ('" & tbNombre.Text & "','" & tbDescripcion.Text & "'," & tbTiempoEstimado.Text & "," & tbPrecio.Text & ");") Then
            MessageBox.Show("Se ha insertado con éxito")
            ActualizazDataGridView()
        Else
            MessageBox.Show("No se ha insertado con éxito")
        End If
        Me.limpiar()
    End Sub

    Private Sub Modificar()

        If miDB.EjecutarComandoUpdate("UPDATE tratamientos SET nombre='" & tbNombre.Text & "', descripcion='" & tbDescripcion.Text & "', tiempoEstimado='" & tbTiempoEstimado.Text & "', precio='" & tbPrecio.Text & "' WHERE idTratamiento=" & dgwLista.CurrentRow.Cells("idTratamiento").Value & ";") Then
            MessageBox.Show("Se ha modificado correctamente")
            ActualizazDataGridView()
        Else
            MessageBox.Show("No se ha modificado correctamente")
        End If
        Me.limpiar()
    End Sub

    Private Sub Eliminar()
        Dim resultado As DialogResult = MessageBox.Show("¿Realmente desea eliminar este tratamiento?", "Eliminar Tratamiento", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        Select Case resultado
            Case Windows.Forms.DialogResult.Yes
                miDB.EjecutarComandoDelete("DELETE FROM tratamientos WHERE idTratamiento=" & dgwLista.CurrentRow.Cells("idTratamiento").Value & ";")
                ActualizazDataGridView()
            Case Windows.Forms.DialogResult.No
                'Descartar los cambios 
        End Select
        Me.limpiar()
    End Sub

    Private Sub dgwLista_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgwLista.CellContentClick
        tbNombre.Text = dgwLista.CurrentRow.Cells("nombre").Value.ToString
        tbDescripcion.Text = dgwLista.CurrentRow.Cells("descripcion").Value.ToString
        tbTiempoEstimado.Text = dgwLista.CurrentRow.Cells("tiempoEstimado").Value.ToString
        tbPrecio.Text = dgwLista.CurrentRow.Cells("precio").Value.ToString
    End Sub

    Private Sub Buscar()
        dgwLista.Rows.Clear()
        miDB.EjecutarComandoSelect("select * from tratamientos where nombre like '%" & tbBuscar.Text & "%'")
        Dim linea(5) As String
        While miDB.lector.Read()
            linea(0) = miDB.lector("idTratamiento").ToString
            linea(1) = miDB.lector("nombre").ToString
            linea(2) = miDB.lector("descripcion").ToString
            linea(3) = miDB.lector("tiempoEstimado").ToString
            linea(4) = miDB.lector("precio").ToString
            dgwLista.Rows.Add(linea)
        End While
        miDB.Desconectar()
    End Sub

    Private Sub ActualizazDataGridView()
        dgwLista.Rows.Clear()
        miDB.EjecutarComandoSelect("select * from tratamientos")
        Dim linea(5) As String
        While miDB.lector.Read()
            linea(0) = miDB.lector("idTratamiento").ToString
            linea(1) = miDB.lector("nombre").ToString
            linea(2) = miDB.lector("descripcion").ToString
            linea(3) = miDB.lector("tiempoEstimado").ToString
            linea(4) = miDB.lector("precio").ToString
            dgwLista.Rows.Add(linea)
        End While
        miDB.Desconectar()
    End Sub

#Region "eventos picture"

    Private Sub Picture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBoxBuscar.Click, PictureBoxBorrar.Click, PictureBoxModificar.Click, PictureBoxGuardar.Click

        Dim auxPicture As PictureBox = CType(sender, PictureBox)

        Select Case auxPicture.Name
            Case PictureBoxBuscar.Name
                Me.Buscar()
            Case PictureBoxBorrar.Name
                Me.Eliminar()
            Case PictureBoxGuardar.Name
                Me.Insertar()
            Case PictureBoxModificar.Name
                Me.Modificar()

        End Select

    End Sub

    Private Sub Picture_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBoxBuscar.MouseHover, PictureBoxBorrar.MouseHover, PictureBoxModificar.MouseHover, PictureBoxGuardar.MouseHover

        Dim auxPicture As PictureBox = CType(sender, PictureBox)

        Select Case auxPicture.Name
            Case PictureBoxBuscar.Name
                PictureBoxBuscar.BackgroundImage = Image.FromFile("Img/Botones/BBuscarCon.png")
            Case PictureBoxBorrar.Name
                PictureBoxBorrar.BackgroundImage = Image.FromFile("Img/Botones/BBorrarCon.png")
            Case PictureBoxGuardar.Name
                PictureBoxGuardar.BackgroundImage = Image.FromFile("Img/Botones/BGuardarCon.png")
            Case PictureBoxModificar.Name
                PictureBoxModificar.BackgroundImage = Image.FromFile("Img/Botones/BModificarCon.png")

        End Select

    End Sub

    Private Sub Picture_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBoxBuscar.MouseLeave, PictureBoxBorrar.MouseLeave, PictureBoxModificar.MouseLeave, PictureBoxGuardar.MouseLeave

        Dim auxPicture As PictureBox = CType(sender, PictureBox)

        Select Case auxPicture.Name
            Case PictureBoxBuscar.Name
                PictureBoxBuscar.BackgroundImage = Image.FromFile("Img/Botones/BBuscarSin.png")
            Case PictureBoxBorrar.Name
                PictureBoxBorrar.BackgroundImage = Image.FromFile("Img/Botones/BBorrarSin.png")
            Case PictureBoxGuardar.Name
                PictureBoxGuardar.BackgroundImage = Image.FromFile("Img/Botones/BGuardarSin.png")
            Case PictureBoxModificar.Name
                PictureBoxModificar.BackgroundImage = Image.FromFile("Img/Botones/BModificarSin.png")
        End Select

    End Sub

#End Region

End Class